﻿using System.Globalization;
// ColorPicker, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace ColorPicker
{
    public sealed class CleanString
    {
        private static TextInfo ti = null;
        static CleanString()
        {
            //
            ti = new CultureInfo("en-US", false).TextInfo;
            //
        }

        public static string CleanName(string s)
        {
            s = s.Replace('_', ' ').Replace('.', ' ').Replace("#", " ").Replace("/", " ").Replace("\\", " ").Replace("-", " ").Replace("'", " ").Replace("(", " ").Replace(")", " ").Replace("  ", " ").Replace("  ", " ");
            s = s.Trim();
            if ("" == s)
            {
                s = "_";
            }
            else
            {
                string[] tokens = s.Split(new[] { ' ' });
                if (tokens.Length > 1)
                {
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        tokens[i] = ToCamelIfNeeded(tokens[i]);
                    }
                    s = string.Join("", tokens);
                }
            }
            //
            //
            return s;
        }



        public static string ToCamelIfNeeded(string src)
        {
            if ((src == ti.ToLower(src)) || (src == ti.ToUpper(src)))
            {

                src = ti.ToTitleCase(ti.ToLower(src));
            }
            return src;
        }

    }//class
}//namespace
